

//
//  ZYConmentDetailsController.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/3.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYConmentDetailsController.h"
#import "ZYConmentDetailsCell.h"
#import "ZYConmentDetailsModel.h"
#define kCell @"cell"
#import "HNTeacgerEvaluateDetailController.h"
#import "HMTeacgerEvaluateDetailVC.h"
#import "ReplyView.h"
#import "ReceiveFlowerView.h"
@interface ZYConmentDetailsController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property(nonatomic,strong)UILabel *nameLbel;
@property(nonatomic,strong)UILabel *teacherLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UIButton *seeButton;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,strong) NSDictionary *dicts;


@end

@implementation ZYConmentDetailsController

- (NSMutableArray *)dataSource {
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"评价详情";
     [self setNetWork];
    [self setUpSuperView];
    
   
    
   
    
    
}




- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
}


- (void)setNetWork{
    
    //http://192.168.0.113:8080/StarShine/ws/trainLesson.do?action=getCommentDetial&commentId=847&uid=285
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getCommentDetial&commentId=%@&uid=%@&replyId=%@",self.commentId,self.uid,self.ID];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
           
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            self.dicts = t;
            
            self.teacherLabel.text = [NSString stringWithFormat:@"学生：%@",self.petName];
            self.nameLbel.text = [NSString stringWithFormat:@"课程名称：%@",[self.dicts valueForKey:@"lessonName"]];
            self.timeLabel.text = [NSString stringWithFormat:@"%@",[self.dicts valueForKey:@"createTime"]];
            
            NSArray *arr = [t valueForKey:@"lessonComments"];
            
            for (NSDictionary *dict in arr) {
                
                ZYConmentDetailsModel *model = [ZYConmentDetailsModel new];
                
                
                model.ID = [dict valueForKey:@"id"];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataSource addObject:model];
                
            }
            
        }
        
        //            // 奖励红花的弹窗
        NSString *str = Obj[@"t"][@"param"];
        if ( ![str isKindOfClass:[NSNull class]] && str.length > 0) {
            
            [ReceiveFlowerView receiveFlowerWithMsg:str Complete:nil];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
}







- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:(UITableViewStyleGrouped)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[ZYConmentDetailsCell  class] forCellReuseIdentifier:kCell];
    self.tableView.estimatedRowHeight = 200;
    //    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.tableFooterView = [UIView new];
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    self.allView.backgroundColor = [UIColor whiteColor];
    
    self.nameLbel = [UILabel new];
    self.nameLbel.font = [UIFont systemFontOfSize:14];
    self.nameLbel.textColor = ColorRGB(51, 51, 51);
    [self.allView addSubview:self.nameLbel];
    
    
    self.teacherLabel = [UILabel new];
    self.teacherLabel.font = [UIFont systemFontOfSize:14];
    self.teacherLabel.textColor = ColorRGB(51, 51, 51);
    [self.allView addSubview:self.teacherLabel];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    self.timeLabel.textColor = ColorRGB(102,102,102);
    [self.allView addSubview:self.timeLabel];
    
    
    
    self.seeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.seeButton setTitle:@"查看评语  >" forState:(UIControlStateNormal)];
    [self.seeButton setTitleColor:ColorRGB(51, 51, 51) forState:(UIControlStateNormal)];
    self.seeButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.allView addSubview:self.seeButton];
    
    
    
    self.nameLbel.text = @"评价课程名称";
    [self.nameLbel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.offset(8);
        make.top.offset(10);
        
        
        
    }];
    
    
    self.teacherLabel.text = @"张宇老师";
    [self.teacherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.nameLbel);
        make.top.equalTo(self.nameLbel.mas_bottom).offset(8);
        
    }];
    
    self.timeLabel.text = @"2015.10.11";
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.teacherLabel);
        make.top.equalTo(self.teacherLabel.mas_bottom).offset(8);
        
        
    }];
    
    
    
    [self.seeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.allView).offset(-8);
        make.top.centerY.equalTo(self.teacherLabel);
    }];
    
   
    
    [self.seeButton addTarget:self action:@selector(seeButtonTowAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    self.tableView.tableHeaderView = self.allView;
    
    
    
    
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
    
}



















- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYConmentDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    
    if (self.dataSource.count > indexPath.row) {
          ZYConmentDetailsModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYConmentDetailsModel:model];
        
    }
    
  
    [cell.seeButton addTarget:self action:@selector(seeButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
   
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

#pragma mark --- 查看方法的实现
- (void)seeButtonTowAction:(UIButton *)sender{
    

    
    HMTeacgerEvaluateDetailVC *teacgVc = [HMTeacgerEvaluateDetailVC new];
//    HNTeacgerEvaluateDetailController *teacgVc = [HNTeacgerEvaluateDetailController new];
    teacgVc.ID = [NSString stringWithFormat:@"%@",[self.dicts valueForKey:@"id"]];
    teacgVc.SubjectTitle = [NSString stringWithFormat:@"%@",[self.dicts valueForKey:@"lessonName"]];
    teacgVc.lessonId = [NSString stringWithFormat:@"%@",[self.dicts valueForKey:@"lessonId"]];
    teacgVc.enrollId = [NSString stringWithFormat:@"%@",[self.dicts valueForKey:@"enrollId"]];
    teacgVc.time = [NSString stringWithFormat:@"%@", [self.dicts valueForKey:@"isRead"]];
    [self.navigationController pushViewController:teacgVc animated:YES];
    
    
    
    
    
}














#pragma mark --- 评论方法的实现
- (void)seeButtonAction:(UIButton *)sender{

    ZYConmentDetailsCell *cell = (ZYConmentDetailsCell *)sender.superview.superview;

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ZYConmentDetailsModel *model = self.dataSource[indexPath.row];
    

    ReplyView *View = [[ReplyView alloc] initReplyView];
    View.textFieldBlock = ^(NSString *text) {

        if ([text stringByReplacingOccurrencesOfString:@" " withString:@""].length <= 0) {
            [SVProgressHUD showSuccessWithStatus:@"请输入评论内容"];
        } else {

            [SVProgressHUD showWithStatus:@"上传中,请稍候"];




            NSDictionary *Params = @{
                                     @"action":@"saveCommentReply",
                                     @"commentId":[NSString stringWithFormat:@"%@",model.commentId],
                                     @"content":text,
                                     @"replyId":[NSString stringWithFormat:@"%@",model.ID],
                                     @"replyUid":[NSString stringWithFormat:@"%@",[model.poster valueForKey:@"uid"]]
                                     };



            [NetManager Post:LLBaseURLs(@"trainLesson.do") Auth:YES Params:Params Success:^(id Obj) {

                [SVProgressHUD dismiss];

                [self.tableView.mj_header beginRefreshing];



            } Fail:^(NSError *Error) {
                [SVProgressHUD dismiss];
            }];


        }

    };












}








//将要显示Cell的时候执行该代理方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //补齐分割线、两边延长
    [self replenishLine:cell];
}

//补齐分割线、两边延长
-(void)replenishLine:(UIView *)view{
    
    if ([view respondsToSelector:@selector(setSeparatorInset:)])
    {
        [(UITableViewCell *)view setSeparatorInset:UIEdgeInsetsZero];//注意这里：必须将view强制转换成（UITableViewCell ＊）类，不然会报错。
    }
    if ([view respondsToSelector:@selector(setLayoutMargins:)])
    {
        [view setLayoutMargins:UIEdgeInsetsZero];
    }
}









@end
